package com.compress4j.arithmetic.ppm;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
 * Main class for compression decompression PPM :<br>
 * takes the parameters and launch compression/decompression
 * @author b.pierre
 *
 */
public class PPMCompressor {
	/**
	 * copy an input stream into an ouputstream
	 * @param in
	 * @param out
	 * @throws IOException
	 */
	public static void copy(InputStream in, OutputStream out) throws IOException {
	    try{
		    while (true) {
		      int datum = in.read();
		      if (datum == -1){
		        break;
		      }
		      out.write(datum);
		    }
	    }finally{
	    	in.close();
		    out.close();
	    }
	}
	/**
	 * compress a file to another with parmeter high and low
	 * @param fileSrc
	 * @param fileTarget
	 * @param high
	 * @param low
	 * @throws IOException
	 */
	public void compress(String fileSrc,String fileTarget,int high, int low) throws IOException{
		InputStream in = new BufferedInputStream(new FileInputStream(fileSrc));
		OutputStream out = new BufferedOutputStream(new FileOutputStream(fileTarget));
		compress(in, out, high, low);
	}
	/**
	 * please give me Buffered Stream
	 * @param high
	 * @param low
	 * @param in
	 * @param out
	 * @throws IOException
	 */
	public void compress(InputStream in, OutputStream out,int high, int low)
			throws IOException {
		copy(in,new PPMOutputStream(out,high,low));
	}
	/**
	 * decompress a file to another
	 * @param fileSrc
	 * @param filetarget
	 * @throws IOException
	 */
	public void decompress(String fileSrc,String filetarget) throws IOException{
		InputStream in = new BufferedInputStream(new FileInputStream(fileSrc));
		OutputStream out = new BufferedOutputStream(new FileOutputStream(filetarget));
		decompress(in, out);
	}
	/**
	 * please give me Buffered Stream
	 * @param in
	 * @param out
	 * @throws IOException
	 */
	public void decompress(InputStream in, OutputStream out) throws IOException {
		copy(new PPMInputStream(in),out);
	}
	public static void main(String[] args) {
		if(args.length<1){
			System.out.println("file [PPMhigh [PPMlow]]");
			return;
		}
		int high = 5;
		int low = 0;
		boolean press;
		String fileSrc = args[0];
		String fileTarget;
		
		if(fileSrc.endsWith(".bppm")){
			press = false;
			fileTarget = fileSrc.substring(0,fileSrc.length()-".bppm".length());
		}else{
			press = true;
			if(args.length>1){
				high  = Integer.parseInt(args[1]);
				if(high>32){
					System.out.println("PPMhigh is too big : max 32");
					return;
				}
				if(high<0){
					System.out.println("PPMhigh must be positive");
					return;
				}
			}
			if(args.length>2){
				low  = Integer.parseInt(args[2]);
				if(low>high){
					System.out.println("PPMlow is too big, must be less than PPMHigh");
					return;
				}
				if(low<0){
					System.out.println("PPMlow must be positive");
					return;
				}
			}else{
				low = 0;
			}
			fileTarget = fileSrc+".bppm";
		}
		
		if(new File(fileTarget).exists()){
			System.out.println(press?"Compressed file already exists":"Original file already exists");
			return;
		}
		try {
			if(press){
				new PPMCompressor().compress(fileSrc, fileTarget,high,low);
			}else{
				new PPMCompressor().decompress(fileSrc, fileTarget);
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
